
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TQNINH
 */
class StudentRegistration extends javax.swing.JFrame {
    private Connection con = null;
    private final String url = "jdbc:sqlserver://";
    private final String serverName = "localhost";
    private final String portName = "1433";
    private final String instanceName = "SQL11";
    private final String databaseName = "AssKidsZoneSchool";
    private String userName = "sa";
    private String password = "";
    private String sqlStr = "";
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    /**
     * Creates new form StudentRegistration
     */
    public StudentRegistration() {
        try {            
            //<editor-fold defaultstate="collapsed" desc="Driver 4">
            //
            //Driver 4 - 1
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            //Driver 4 - 1
            con = DriverManager.getConnection(this.getConnectionUrl(), userName, password);
            //
            //</editor-fold>
            if(con != null) {
                JOptionPane.showMessageDialog(null, "Connection is successful!");
            }
        } catch(ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Class does not found!");
            JOptionPane.showMessageDialog(null, "Connection is not successful!");
            System.exit(0);
            e.printStackTrace();
        } catch(SQLException e) {
            if(con == null) {
                JOptionPane.showMessageDialog(null, "Connection is not successful!");
            }
            e.printStackTrace();
        }
        initComponents();
        this.fillData();
        this.getResultSet();
    }
    
    private String getConnectionUrl() {
        return url + serverName + ":" + portName + ";instanceName=" + instanceName + ";databaseName=" + databaseName;
    }
    
    private void getResultSet() {
        pstmt = null;
        rs = null;
        sqlStr = "SELECT * FROM Student";
        try {
            pstmt = con.prepareStatement(sqlStr, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = pstmt.executeQuery();
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    private void fillData() {
        cbbStandard.removeAllItems();
        cbbFees.removeAllItems();
        pstmt = null;
        rs = null;
        try {
            //Standard
            sqlStr = "SELECT * FROM Standard";
            pstmt = con.prepareStatement(sqlStr);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                cbbStandard.addItem(rs.getString(2));
            }
            //Fee
            sqlStr = "SELECT * FROM Fee";
            pstmt = con.prepareStatement(sqlStr);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                cbbFees.addItem(rs.getString(2));
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    private void fillRecord(ResultSet rs) {
        try {
            txtId.setText(rs.getString("SCode"));
            txtDate.setText(rs.getString("SDateCreate1"));
            txtName.setText(rs.getString("SName"));
            txaAddress.setText(rs.getString("SAddress"));
            txtParentName.setText(rs.getString("SParentName"));
            txtContactNo.setText(rs.getString("SContactNo"));
            cbbStandard.setSelectedIndex(rs.getInt("StandardId"));
            cbbFees.setSelectedIndex(rs.getInt("FId"));
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblStudent = new javax.swing.JLabel();
        pnlInformation = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblParentName = new javax.swing.JLabel();
        lblContactNo = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        scpAddress = new javax.swing.JScrollPane();
        txaAddress = new javax.swing.JTextArea();
        txtParentName = new javax.swing.JTextField();
        txtContactNo = new javax.swing.JTextField();
        lblStandard = new javax.swing.JLabel();
        cbbStandard = new javax.swing.JComboBox();
        lblFees = new javax.swing.JLabel();
        cbbFees = new javax.swing.JComboBox();
        lblId = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtDate = new javax.swing.JTextField();
        btnFirst = new javax.swing.JButton();
        btnPrevious = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Student Record View");
        setMinimumSize(new java.awt.Dimension(685, 370));
        setResizable(false);
        getContentPane().setLayout(null);

        lblStudent.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblStudent.setForeground(new java.awt.Color(204, 0, 51));
        lblStudent.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblStudent.setText("STUDENT DETAILS");
        lblStudent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        getContentPane().add(lblStudent);
        lblStudent.setBounds(10, 10, 660, 50);

        pnlInformation.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        pnlInformation.setLayout(null);

        lblName.setText("Name:");
        pnlInformation.add(lblName);
        lblName.setBounds(20, 70, 31, 14);

        lblAddress.setText("Address:");
        pnlInformation.add(lblAddress);
        lblAddress.setBounds(20, 110, 43, 14);

        lblParentName.setText("Parent's Name:");
        pnlInformation.add(lblParentName);
        lblParentName.setBounds(330, 70, 73, 14);

        lblContactNo.setText("Contact No:");
        pnlInformation.add(lblContactNo);
        lblContactNo.setBounds(330, 110, 58, 14);

        txtName.setEditable(false);
        pnlInformation.add(txtName);
        txtName.setBounds(80, 60, 230, 25);

        txaAddress.setEditable(false);
        txaAddress.setColumns(20);
        txaAddress.setRows(5);
        scpAddress.setViewportView(txaAddress);

        pnlInformation.add(scpAddress);
        scpAddress.setBounds(80, 100, 230, 105);

        txtParentName.setEditable(false);
        pnlInformation.add(txtParentName);
        txtParentName.setBounds(410, 60, 230, 25);

        txtContactNo.setEditable(false);
        txtContactNo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtContactNoKeyTyped(evt);
            }
        });
        pnlInformation.add(txtContactNo);
        txtContactNo.setBounds(410, 100, 230, 25);

        lblStandard.setText("Standard:");
        pnlInformation.add(lblStandard);
        lblStandard.setBounds(330, 150, 50, 14);

        pnlInformation.add(cbbStandard);
        cbbStandard.setBounds(410, 140, 230, 25);

        lblFees.setText("Fees:");
        pnlInformation.add(lblFees);
        lblFees.setBounds(330, 190, 27, 14);

        pnlInformation.add(cbbFees);
        cbbFees.setBounds(410, 180, 230, 25);

        lblId.setText("Id:");
        pnlInformation.add(lblId);
        lblId.setBounds(20, 30, 14, 14);

        lblDate.setText("Date:");
        pnlInformation.add(lblDate);
        lblDate.setBounds(330, 30, 27, 14);

        txtId.setEditable(false);
        pnlInformation.add(txtId);
        txtId.setBounds(80, 20, 230, 25);

        txtDate.setEditable(false);
        pnlInformation.add(txtDate);
        txtDate.setBounds(410, 20, 230, 25);

        getContentPane().add(pnlInformation);
        pnlInformation.setBounds(10, 80, 660, 220);

        btnFirst.setText("First");
        btnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFirstActionPerformed(evt);
            }
        });
        getContentPane().add(btnFirst);
        btnFirst.setBounds(10, 310, 80, 25);

        btnPrevious.setText("Previous");
        btnPrevious.setEnabled(false);
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });
        getContentPane().add(btnPrevious);
        btnPrevious.setBounds(110, 310, 80, 25);

        btnNext.setText("Next");
        btnNext.setEnabled(false);
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        getContentPane().add(btnNext);
        btnNext.setBounds(210, 310, 80, 25);

        btnLast.setText("Last");
        btnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLastActionPerformed(evt);
            }
        });
        getContentPane().add(btnLast);
        btnLast.setBounds(310, 310, 80, 25);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtContactNoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtContactNoKeyTyped
        // TODO add your handling code here:
        String numbers = "0123456789 ";
        if(numbers.indexOf(evt.getKeyChar()) == -1) {
            evt.consume();
            JOptionPane.showMessageDialog(null, "Contact No field accepts numberic value only!");
        }
    }//GEN-LAST:event_txtContactNoKeyTyped

    private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFirstActionPerformed
        // TODO add your handling code here:
        try {
            rs.first();
            btnNext.setEnabled(true);
            btnPrevious.setEnabled(false);
            fillRecord(rs);
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnFirstActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        try {
            rs.relative(1);
            if(!rs.isAfterLast()) {
                fillRecord(rs);
                btnNext.setEnabled(true);
                btnPrevious.setEnabled(true);
            } else {
                JOptionPane.showMessageDialog(null, "Dang o du lieu cuoi cung, khong the next nua");
                rs.last();
                btnNext.setEnabled(false);
                btnPrevious.setEnabled(true);
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
        // TODO add your handling code here:
        try {
            rs.relative(-1);
            if(!rs.isBeforeFirst()) {
                fillRecord(rs);
                btnNext.setEnabled(true);
                btnPrevious.setEnabled(true);
            } else {
                JOptionPane.showMessageDialog(null, "Dang o du lieu dau tien, khong the previous nua");
                rs.first();
                btnNext.setEnabled(true);
                btnPrevious.setEnabled(false);
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnPreviousActionPerformed

    private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLastActionPerformed
        // TODO add your handling code here:
        try {
            rs.last();
            btnNext.setEnabled(false);
            btnPrevious.setEnabled(true);
            fillRecord(rs);
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnLastActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StudentRegistration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new StudentRegistration().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JComboBox cbbFees;
    private javax.swing.JComboBox cbbStandard;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblContactNo;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblFees;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblParentName;
    private javax.swing.JLabel lblStandard;
    private javax.swing.JLabel lblStudent;
    private javax.swing.JPanel pnlInformation;
    private javax.swing.JScrollPane scpAddress;
    private javax.swing.JTextArea txaAddress;
    private javax.swing.JTextField txtContactNo;
    private javax.swing.JTextField txtDate;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtParentName;
    // End of variables declaration//GEN-END:variables
}
